<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>推荐位管理</el-breadcrumb-item>
        <el-breadcrumb-item> {{ $route.query.name }} </el-breadcrumb-item>
        <el-breadcrumb-item>人物详情</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        :option="option"
        v-model="form"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
      >
        <template slot="last" slot-scope="scope">
          {{ scope.row.update_time }}
          <el-tag>{{ scope.row.edit_account_name }}</el-tag>
        </template>
        <template slot="menuRight">
          <el-tag>数据共{{ data.length }}条</el-tag>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { catInset as baseURL,catRecommend } from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10,
      },
      type: true,
      data: [],
      totalData: [],
      params: {},
      form: {},
      option: {
        addBtn: false,
        editBtn: true,
        delBtn: false,
        index: true,
        indexLabel: "编号",
        column: [ 
          {
            label: "人物id",
            prop: "inset_id",
            type: "number",
            search: true,
            placeholder: "书籍id",
          },
          {
            label: "人物名称",
            prop: "role_name",
            type: "input",
            search: false,
            placeholder: "人物名称",
            editDisplay: false, 
          },
          {
            label: "关联书籍名称",
            prop: "book_name",
            type: "input",
            search: false,
            placeholder: "关联书籍名称",
            editDisplay: false, 
          },
          {
            label: "推荐等级",
            prop: "level",
            type: "select",
            search: false, 
            dicData:[
                {
                    label:'N',
                    value: 0
                },
                {
                    label:'R',
                    value: 1
                },
                {
                    label:'SR',
                    value: 2
                },
                {
                    label:'SSR',
                    value: 3
                },
                {
                    label:'UR',
                    value: 4
                },
            ],
            props: {
              label: "label",
              value: "value",
            },
            editDisplay: false, 
          },
          {
            label: "排序",
            prop: "sort",
            type: "number",
            search: false,
            placeholder: "排序",
          },
          {
            label: "最后操作信息",
            prop: "last",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: false,
            readonly: true,
            slot: true,
          },
        ],
      },
    };
  },
  methods: {
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        `${baseURL.getListByRecommendId}/${this.$route.query.id}`,
        this.params,
        "GET"
      );
      response.data.forEach((e, index) => (e.sort = index + 1));
      this.data = response.data;
      this.totalData = response.data;
      this.loading = false;
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      // debugger;
      this.totalData.forEach((e) => {
        if (e.role_name == row.role_name) {
          e.sort = row.sort;
          e.inset_id = row.inset_id;
        }
      });
      this.totalData.sort((d1, d2) => d1.sort - d2.sort);
      let userInfo = this.$store.state.user.userInfo;  
      let recommend = {
          id:this.$route.query.id,
          inset_id:this.totalData.map((e) => e.inset_id).join(","),
          edit_account_name : userInfo.username,
          style:0
      }
      let data = await common( `${catRecommend.updateTwo}`, recommend, null, "POST");
      if (data == null) {
        this.getList();
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
      if (params.inset_id) {
        this.data = this.totalData.filter(
          (e) => (e.inset_id + "").indexOf(params.inset_id) != -1
        );
      } else {
        this.data = this.totalData;
      }
      if (done) done();
    },
  },
};
</script>

<style>
</style>